<?php
/**
 * WpSimpleView.php
 * 
 * This file contains the WpSimpleView class, a simple php view  
 * to be used in WildPack mvc
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */

/**
 * The interface to implement
 */
require_once (dirname(__FILE__).'/IWpView.php');

/**
 * This is a simple php view  
 * to be used in WildPack mvc
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */
class WpSimpleView implements IWpView
{
	/**
	 * @var The assoc data array
	 */
	private $data = array();
	
	/**
	 * @var The path to the view files
	 */
	private $viewpath = 'view/';
	
	/**
	 * @var The default view which is used if no view was specified
	 */
	public $DefaultView = 'Index';
	
	/**
	 * @var The name of the view to use
	 */
	public $Viewname = 'Index';
	
	/**
	 * The class constructor
	 */
	public function __construct($config = null)
	{
		if (is_null($config)) {
			$config = array();
		}
		
		$this->viewpath = array_key_exists('viewpath', $config) ? $config['viewpath'] : $this->viewpath;
	}

	/**
   * Assigns data to the view
   * @param mixed $data The data to assign to the view
   */
	public function assign($data)
	{
		foreach ($data as $key => $dentry) {
			$this->data[$key] = $dentry;
		}
	}
	
	/**
   * Renders the view and returns the content
   * @param object $viewname[optional] The name of the view to render
   * @return string The rendered view as string
   */
	public function fetch($viewname = '')
	{
		if (is_null($viewname) || $viewname === '') {
			$viewname = $this->DefaultView;
		}
		
		$vfpath = $this->viewpath.$viewname.'.php';
		if (!file_exists($vfpath)) {
			return '';
		}
		
		ob_start();
		require($vfpath);
		$body = ob_get_contents();
		ob_end_clean();
		
		return $body;
	}
	 
	/**
	 * Retrieve an attributes value
	 * @param string $attribute The name of the attribute to retrieve
	 * @return mixed The attributes value
	 */
	public function get($attribute)
	{
		if (!array_key_exists($attribute, $this->data)) {
			return null;
		}
		
		return $this->data[$attribute];
	}
	
	/**
	 * Write an attribute the to the view
	 * @param string $attribute The name of the attribute to write it's value
	 */
	public function write($attribute)
	{
		print($this->get($attribute));
	}
	
	/**
   * Renders the view
   * @param string $viewname The name of the view to render
   * @param object $response The response to write the rendered view to
   */
	public function render($response)
	{
		if (is_null($this->Viewname) || $this->Viewname === '') {
			$this->Viewname = $this->DefaultView;
		}
		
		$body = $this->fetch($this->Viewname);
		
		$response->write($body);
		$response->flush();
	}
	
	/**
   * Specifies the name of the view to use
   * @param string $viewname The name of the view to use
   */
	public function setViewname($viewname)
	{
		$this->Viewname = $viewname;
	}
}

/* End of file WpSimpleView.php */
/* Location: ./wildpack/mvc/view/WpSimpleView.php */
